package com.example.elmspringboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elmspringboot.domain.Business;
import com.example.elmspringboot.domain.User;
import com.example.elmspringboot.mapper.UserMapper;
import com.example.elmspringboot.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Override
    public Boolean login(String name, String password) {
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        List<User> users = userMapper.selectList(wrapper.eq(name != null, User::getName, name));
        User user = users.get(0);
        if(user==null){
            return false;
        }
        return user.getPassword().equals(password);
    }

    @Override
    public User register(String name, String password) {
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        List<User> users = userMapper.selectList(wrapper.eq(name != null, User::getName, name));
        if(users.isEmpty()){
            User user =new User();
            user.setImgUrl("/images/user/饺子.jpg");
            user.setName(name);
            user.setPassword(password);
            user.setSex("男");
            int insert = userMapper.insert(user);
            if (insert==1){
                return userMapper.selectById(user.getId());
            }
        }
        return null;
    }

    @Override
    public User getUserByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        return userMapper.selectOne(wrapper.eq("name", name));

    }
}
